<%@page import="com.liferay.portal.kernel.util.GetterUtil"%>
<%@page import="javax.portlet.PortletSession"%>
<%@page import="com.liferay.portlet.PortletPreferencesFactoryUtil"%>
<%@page import="javax.portlet.PortletPreferences"%>
<%@ include file="/html/init.jsp"%>

<%@page import="com.liferay.portal.kernel.util.LocaleUtil"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="com.liferay.portal.kernel.dao.search.SearchEntry"%>
<%@page import="java.io.File"%>

<%@page import="com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil"%>
<%@page import="com.liferay.portlet.shopping.model.ShoppingItem"%>
<%@page import="com.liferay.portlet.shopping.model.ShoppingCart"%>
<%@page import="com.liferay.portal.kernel.dao.search.ResultRow"%>
<%@page import="com.liferay.portal.kernel.dao.search.SearchContainer"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.slayer.model.CartItem"%>

<%
	PortletPreferences preferences = renderRequest.getPreferences();

	boolean isCheckoutPage = GetterUtil.getBoolean(preferences
			.getValue("isCheckoutPage", "false"));

	Map<Long, CartItem> items = (HashMap<Long, CartItem>) session
			.getAttribute("USER_CART_ITEMS");

	String slash = File.separator;

	String deleteIcon = themeDisplay.getTheme().getImagesPath() + slash
			+ "application" + slash + "close.png";
%>

<c:choose>
	<c:when test="<%=(items != null && !items.isEmpty())%>">
		<%
			SearchContainer searchContainer = new SearchContainer();

			Iterator<Map.Entry<Long, CartItem>> itr = items.entrySet()
					.iterator();

			List<String> headerNames = new ArrayList<String>();

			headerNames.add("Item");
			headerNames.add("Qty");
			headerNames.add("Amount");

			if (!isCheckoutPage) {
				headerNames.add("<img src=" + deleteIcon + ">");
			}

			searchContainer.setHeaderNames(headerNames);

			searchContainer.setHover(false);
			int total = items.size();

			searchContainer.setTotal(total);

			List<ResultRow> resultRows = searchContainer
					.getResultRows();

			PortletURL deleteItemURL = renderResponse.createActionURL();
			deleteItemURL.setParameter(ActionRequest.ACTION_NAME,
					"removeFromCart");

			PortletURL emptyCartURL = renderResponse.createActionURL();
			emptyCartURL.setParameter(ActionRequest.ACTION_NAME,
					"emptyCart");

			PortletURL checkOutURL = renderResponse.createActionURL();
			checkOutURL.setParameter(ActionRequest.ACTION_NAME,
					"checkOut");

			int i = 0;
			int totalItems = 0;
			int totalAmount = 0;

			NumberFormat currencyFormatter = NumberFormat
					.getCurrencyInstance(LocaleUtil.getDefault());
			currencyFormatter.setMaximumFractionDigits(0);

			while (itr.hasNext()) {
				Map.Entry<Long, CartItem> pairs = (Map.Entry<Long, CartItem>) itr
						.next();
				long itemId = pairs.getKey();
				CartItem cartItem = pairs.getValue();
				ShoppingItem shoppingItem = ShoppingItemLocalServiceUtil
						.fetchShoppingItem(itemId);

				if (Validator.isNotNull(shoppingItem)) {
					ResultRow row = new ResultRow(
							(CartItem) pairs.getValue(), itemId, i++);
					row.addText(shoppingItem.getName()
							+ "<br/><i><font color='grey'>Collected: "
							+ IConstants.FREQUENCY[cartItem
									.getFrequency() - 1] + "</i>");
					row.addText("right", SearchEntry.DEFAULT_VALIGN,
							Integer.toString(cartItem.getQty()));
					row.addText("right", SearchEntry.DEFAULT_VALIGN,
							currencyFormatter.format((int) cartItem
									.getAmount()));
					deleteItemURL.setParameter("itemId",
							String.valueOf(itemId));
					String removeLink = "<a href=\""
							+ deleteItemURL.toString() + "\">"
							+ "<img src=" + deleteIcon + ">" + "</a>";

					if (!isCheckoutPage) {
						row.addText(removeLink);
					}
					resultRows.add(row);
					totalItems += cartItem.getQty();
					totalAmount += cartItem.getAmount();
				}
			}
		%>
		<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" paginate="<%= false %>" />

		<br />
		<h3>
			Total items:
			<%= totalItems %>
			Amount :
			<fmt:setLocale value="en_US" />
			<fmt:formatNumber value="<%= (int) totalAmount %>" type="currency"
				maxFractionDigits="0" />
		</h3>

		<c:if test="<%= !isCheckoutPage %>">
			<aui:button-row>
				<aui:button type="button" value="Empty Cart"
					href="<%= emptyCartURL.toString() %>" />
				<aui:button type="button" value="CheckOut"
					href="<%= checkOutURL.toString() %>" />
			</aui:button-row>
		</c:if>
	</c:when>
	<c:otherwise>Your cart is empty. Please add your donations.</c:otherwise>
</c:choose>